

package arghhh.servlet;

import arghhh.implementaçao.Sistema;
import arghhh.implementaçao.modelo.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author 200824040166
 */
public class ServletBanda extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    public static Sistema sistema = new Sistema();
    public Banda bandaLogado = new Banda(null, null, null, null, null, null, null, null, null, null, null);

     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {} finally {
            out.close();
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String pagina = request.getParameter("pagina");


        if(pagina.equalsIgnoreCase("Perfilbanda")){
            request.setAttribute("banda", bandaLogado);
            RequestDispatcher d = request.getRequestDispatcher("Banda/Perfilbanda.jsp");
            d.forward(request, response);
            return;
        }

        else if(pagina.equalsIgnoreCase("AlbumBanbanda")){
            request.setAttribute("banda", bandaLogado);
            RequestDispatcher d = request.getRequestDispatcher("Banda/Albumbanda.jsp");
            d.forward(request, response);
            return;
        }

        else if(pagina.equalsIgnoreCase("Fotobanda")){
            request.setAttribute("banda", bandaLogado);
            RequestDispatcher d = request.getRequestDispatcher("Banda/Fotobanda.jsp");
            d.forward(request, response);
            return;
        }

        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         String botao = request.getParameter("botao");

        if(botao.equalsIgnoreCase("Realizar Cadastro")){
            String senha = request.getParameter("password");
            String senhaCheck = request.getParameter("passwordCheck");
            if(!senha.equals(senhaCheck)){
                RequestDispatcher d = request.getRequestDispatcher("index.jsp"); //Direcionar Para Erro
                d.forward(request, response);
                return;
            }
            String login = request.getParameter("username");
            if(sistema.containsLogin(login)){
                RequestDispatcher d = request.getRequestDispatcher("index.jsp"); //Direcionar Para Erro
                d.forward(request, response);
                return;
            }
            String nome = request.getParameter("nome");
            String email = request.getParameter("email");
            String mes = request.getParameter("mes");
            String dia = request.getParameter("dia");
            String ano = request.getParameter("ano");
            String cidade = request.getParameter("cidade");
            String estado = request.getParameter("estado");
            String[] estilos = request.getParameterValues("estilos");
            String[] integrantes = request.getParameterValues("integrantes");
            System.out.println(estilos);
            System.out.println(integrantes);
            try {
                sistema.inserirBanda(login, senha, nome, email, dia, mes, ano, cidade, estado, estilos, integrantes);
            } catch (SQLException ex) {
                Logger.getLogger(ServletBanda.class.getName()).log(Level.SEVERE, null, ex);
            }
            request.setAttribute("banda", sistema.buscarBanda(login));
            bandaLogado = sistema.buscarBanda(login);
            request.setAttribute("banda", bandaLogado);
            RequestDispatcher d = request.getRequestDispatcher("Banda/PerfilBanda.jsp");
            d.forward(request, response);
            return;
        }

        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

